/*
 * Copyright 2020-2021 redragon-erp/赤龙ERP
 * Author redragon/dongbin
 * This file is part of redragon-erp/赤龙ERP.

 * 赤龙ERP官方声明：
 * (1)赤龙ERP的知识产权归软件著作权人所有
 * (2)未经著作权人授权，不得将赤龙ERP用于任何形式的商业用途
 * (3)对侵权的组织或个人，著作权人将保留诉诸法律的权利

 * 赤龙ERP官网详见 <https://www.redragon-erp.com>
 * 赤龙ERP仓库详见 <https://gitee.com/redragon/redragon-erp> 
 * 如遇任何问题，请与著作权人联系
 */
package com.erp.oa.notice.controller;

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.redragon.framework.springmvc.ControllerSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;
import org.redragon.framework.util.ShiroUtil;

import com.erp.hr.service.HrCommonService;
import com.erp.oa.notice.dao.data.DataBox;
import com.erp.oa.notice.dao.model.SysNoticeRecord;
import com.erp.oa.notice.dao.model.SysNoticeRecordCO;
import com.erp.oa.notice.service.SysNoticeRecordService;

@Controller
@RequestMapping("/web/sysNoticeRecord")
public class SysNoticeRecordWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(SysNoticeRecordWebController.class);
    
    //服务层注入
    @Autowired
    private SysNoticeRecordService sysNoticeRecordService;
    @Autowired
    private HrCommonService hrCommonService;
    
    @Override
    public String getExceptionRedirectUrl() {
        // TODO Auto-generated method stub
        return null;
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-04-05 19:47:53
     * @author 
     * @param pages
     * @param sysNoticeRecordCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getSysNoticeRecordList")
    public String getSysNoticeRecordList(Pages pages, SysNoticeRecordCO sysNoticeRecordCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        
        //分页查询数据
        sysNoticeRecordCO.setPublishPerson(this.hrCommonService.getStaffInfo(ShiroUtil.getPrincipal()).getStaffCode());
        List<SysNoticeRecord> sysNoticeRecordList = this.sysNoticeRecordService.getDataObjects(pages, sysNoticeRecordCO);
        
        //业务类型
        Map businessTypeMap = DataBox.getBusinessType();
        //通知等级
        Map noticeLevelMap = DataBox.getNoticeLevel();
        
        //页面属性设置
        model.addAttribute("sysNoticeRecordList", sysNoticeRecordList);
        model.addAttribute("pages", pages);
        model.addAttribute("businessTypeMap", businessTypeMap);
        model.addAttribute("noticeLevelMap", noticeLevelMap);
        
        return "basic.jsp?content=sysNotice/sysNoticeRecordList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-04-05 19:47:53
     * @author 
     * @param sysNoticeRecord
     * @param model
     * @return String
     *
     */
    @RequestMapping("getSysNoticeRecord")
    public String getSysNoticeRecord(SysNoticeRecord sysNoticeRecord, Model model) {
        //TODO: 查询要编辑的数据
        
        //页面属性设置
        model.addAttribute("sysNoticeRecord", sysNoticeRecord);
        
        return "sysNoticeRecord/sysNoticeRecordEdit";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-04-05 19:47:53
     * @author 
     * @param sysNoticeRecord
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editSysNoticeRecord")
    public String editSysNoticeRecord(@Valid SysNoticeRecord sysNoticeRecord, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getSysNoticeRecord";
        }
        
        //TODO: 对当前编辑的对象初始化默认的字段
        
        //保存编辑的数据
        this.sysNoticeRecordService.insertOrUpdateDataObject(sysNoticeRecord);
        //提示信息
        attr.addFlashAttribute("hint", "success");
        
        return "redirect:getSysNoticeRecordList";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-04-05 19:47:53
     * @author 
     * @param sysNoticeRecord
     * @return String
     *
     */
    @RequestMapping("deleteSysNoticeRecord")
    public String deleteSysNoticeRecord(SysNoticeRecord sysNoticeRecord, RedirectAttributes attr) {
        //删除数据前验证数据
        if(sysNoticeRecord.getRecordId()!=null) {
            //删除数据
            this.sysNoticeRecordService.deleteDataObject(sysNoticeRecord);
            //提示信息
            attr.addFlashAttribute("hint", "success");
        }
        
        return "redirect:getSysNoticeRecordList";
    }
    
    
    
    @RequestMapping("readSysNoticeRecord")
    public String readSysNoticeRecord(Integer recordId, RedirectAttributes attr) {
        //删除数据前验证数据
        if(recordId!=null) {
            //删除数据
            this.sysNoticeRecordService.updateSysNoticeRecordForReadFlag(recordId);
            //提示信息
            attr.addFlashAttribute("hint", "success");
        }
        
        return "redirect:getSysNoticeRecordList";
    }
}
